#ifndef WELLMODEL_H_
#define WELLMODEL_H_

#include "ModelComposite.h"
#include "Position.h"
#include "Players.h"
#include "RowsModel.h"
#include "PieceModel.h"
#include "PieceGeneratorModel.h"

class WellModel : public ModelComposite
{
public:
    WellModel(PlayersPtr players, unsigned int widthOfWell);
    virtual ~WellModel();

    void init();
    void movePiecesDown();

    void movePieceLeft(int playerId);
    void movePieceRight(int playerId);
    void movePieceDown(int playerId);
    void movePieceInstantDown(int playerId);
    void rotatePiece(int playerId);

    virtual void handleEvent(const Event& event);

private:
    bool move(int playerId, const Position& delta);
    bool pushOtherPiecesIfNeeded(int playerId, const Position& delta);
    void addPiece(int playerId);
    void addBlocksFromPiece(int playerId);
    bool okToMove(int playerId);
    bool okToMoveCheckingBlocks(int playerId);
    bool okToRotate(int playerId);
    void replacePieceForPlayer(int playerId);
    void movePieceAsLowAsPossible(int playerId);
    PlayerIds getCollidedPlayers(int playerId);
    void moveCollidingPlayersInstantDown(int playerId);
    void moveAddedPieceUpIfOtherPlayerIsInTheWay(int playerId);

    RowsModel* rows;
    PieceGeneratorModel* pieceGenerator;

    PieceModelsMap pieces;
    PlayersPtr players;
};

#endif
